home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / wed13b.zip / WEEXTSRC.ZI$ / WE_EXT.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  13KB  |  423 lines

  1. /*-------------------------------------------------------------------------*\
  2.  |                                                                         |
  3.  |                                                                         |
  4.  |  WE_EXT.C - A Sample DLL Extension Processor for WinEdit                |
  5.  |                                                                         |
  6.  |                                                                         |
  7. \*-------------------------------------------------------------------------*/
  8. #define STRICT
  9. #define _WINDLL
  10. #include <windows.h>
  11. #include "we_ext.h"
  12. #include "private.h"
  13.  
  14. #define NOREF(a)  {a=a;}
  15.  
  16. HMENU hTrackMenu;
  17. BOOL bWait=TRUE;
  18. BOOL bCapture=TRUE;
  19. char szCommand[256];
  20.  
  21. /*-------------------------------------------------------------------------*\
  22.  |                                                                         |
  23.  |  Function:   WE_ExtensionProc                                           |
  24.  |                                                                         |
  25.  |  Purpose:    WinEdit calls this function with the WEN_* messages and    |
  26.  |              whenever a user-defined menu item or accelerator is        |
  27.  |              accessed.                                                  |
  28.  |                                                                         |
  29.  |  Parameters: HWND hWnd   - WinEdit's window handle                      |
  30.  |                                                                         |
  31.  |              UINT wParam - Message ID.  If wParam is >= WE_EXTFIRST,    |
  32.  |                            the DLL is being requested to perform the    |
  33.  |                            user-defined action.                         |
  34.  |                                                                         |
  35. \*-------------------------------------------------------------------------*/
  36. UINT FAR PASCAL WE_ExtensionProc(HWND hWnd,     /* WinEdit's window handle */
  37.                                  HANDLE hInst,  /* instance identifier     */
  38.                                  UINT wParam,   /* command ID              */
  39.                                  LONG lParam)   /* additional information  */
  40.    {
  41.  
  42.    switch (wParam)
  43.       {
  44.  
  45.       case WEN_LOADMENU:
  46.  
  47.          /*  This is the menu WinEdit will display when there
  48.           *  is at least one document window open.  Return NULL
  49.           *  to use the default WinEdit menu.
  50.           *
  51.           */
  52.  
  53.          return (UINT)LoadMenu(hInst, "MyMenu");
  54.          break;
  55.  
  56.       case WEN_LOADSHORTMENU:
  57.  
  58.          /*  this is the menu WinEdit will display when there
  59.              *  are no document windows open.  Return NULL
  60.              *  to use the default WinEdit menu.
  61.              *
  62.              */
  63.    
  64.             return (UINT)LoadMenu(hInst, "MyShortMenu");
  65.             return NULL;
  66.             break;
  67.    
  68.       case WEN_LOADACCELS:
  69.  
  70.          /*  To re-define the WinEdit command keys, load your
  71.           *  own accelerator table here.  Return NULL to
  72.           *  use the default WinEdit accelerators.
  73.           *
  74.           */
  75.  
  76.          return (UINT)LoadAccelerators (hInst,"MyAccels");
  77.          break;
  78.  
  79.       case WEN_GETWINDOWMENU:
  80.  
  81.          /*  WinEdit needs the handle of the submenu to
  82.           *  append MDI document names to.  The hWnd parameter
  83.           *  is used to send the handle to the main menu.
  84.           *  This message will not be sent if you return
  85.           *  NULL to the WEN_LOADMENU message.
  86.           */
  87.          return (UINT)GetSubMenu ((HMENU)hWnd, WINDOWMENU);
  88.          break;
  89.  
  90.       case WEN_GETMACROMENU:
  91.  
  92.          /*  WinEdit needs the handle of the submenu to
  93.           *  append macro names to.  The hWnd parameter
  94.           *  is used to send the handle to the main menu.
  95.           *  This message will not be sent if you return
  96.           *  NULL to the WEN_LOADMENU message.
  97.           */
  98.          return (UINT)GetSubMenu ((HMENU)hWnd, MACROMENU);
  99.          break;
  100.          
  101.       case WEN_END:
  102.  
  103.          /*  WinEdit is shutting down.  Do any clean-up processing
  104.           *  here.
  105.           */
  106.          if (hTrackMenu)
  107.             {
  108.             DestroyMenu(hTrackMenu);
  109.             hTrackMenu = (HMENU)NULL;
  110.             }
  111.          return TRUE;
  112.          break;
  113.  
  114.       case WEN_INITMENU:
  115.  
  116.          /*  This message is sent before showing any drop down
  117.           *  menu items.  Respond by setting any checkmarks,
  118.           *  graying any inapplicable items, etc.
  119.           *
  120.           */
  121.          return InitMenu(hWnd);
  122.          break;
  123.  
  124.       case WEN_RBUTTONDOWN:
  125.  
  126.          /*  WinEdit sends right button clicks to the
  127.           *  extension processor.  lParam contains the
  128.           *  x and y coordinates of the mouse cursor,
  129.           *  in screen coordinates.
  130.           *
  131.           *  This sample duplicates WinEdit's default action
  132.           *  of putting up a TrackPopupMenu.  You can
  133.           *  modify the menu to include the commands you
  134.           *  use most often.
  135.           */
  136.          if (!hTrackMenu)
  137.             {
  138.  
  139.             hTrackMenu = CreatePopupMenu();
  140.             if (!hTrackMenu)
  141.                break;
  142.                
  143.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEOPEN,"&Open...\tF3");
  144.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILESAVE,"&Save\tF2");                 
  145.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEPRINT,"&Print\tF9");                
  146.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  147.  
  148.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITUNDO,"&Undo\tAlt+BkSp");           
  149.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITREDO,"&Redo\tCtrl+BkSp");          
  150.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  151.  
  152.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCUT,"Cu&t\tShift+Del");           
  153.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCOPY,"&Copy\tCtrl+Ins");           
  154.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITPASTE,"&Paste\tShift+Ins");         
  155.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  156.  
  157.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHFIND,"&Find...\tF5");              
  158.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXT,"&Repeat Last Find\tCtrl+F5");
  159.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHCHANGE,"&Change...\tF6");            
  160.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  161.  
  162.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXTERR,"&Next Error\tShift+F3");    
  163.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHPREVERR,"&Previous Error\tShift+F4");
  164.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  165.  
  166.             AppendMenu(hTrackMenu,MF_STRING,IDM_HELPKEYWORDS,"&Key Word Help\tShift+F1");  
  167.             }
  168.             
  169.          if (hTrackMenu)
  170.             {
  171.             POINT pt;
  172.  
  173.             pt = MAKEPOINT(lParam);
  174.             TrackPopupMenu(hTrackMenu,0,pt.x-10,pt.y-6,0,hWnd,0L);
  175.             }
  176.             
  177.          return TRUE;   
  178.          break;
  179.  
  180.  
  181.       case WEN_RBUTTONDOWNS:
  182.          {
  183.  
  184. #if 1
  185.          /*  This is an example of trapping the right
  186.           *  mouse button.  The #else code is a duplicate
  187.           *  of WinEdit's default action.
  188.           *
  189.           */
  190.  
  191.          char szTemp[64];
  192.  
  193.          MessageBeep(0);
  194.          edEditGetCurrentWord(hWnd,szTemp,63);
  195.          if (szTemp[0])
  196.             WinHelp(hWnd,"WIN31WH.HLP",HELP_KEY,(DWORD)(LPSTR)szTemp);
  197.  
  198.          return TRUE;
  199. #else         
  200.          edHelpKeyWord(hWnd);
  201. #endif
  202.          return TRUE;
  203.          }
  204.  
  205.  
  206.  
  207.       /*  You can define your own commands in the range
  208.        *  WE_EXTFIRST to WE_EXTLAST that can be attached to
  209.        *  menu items or accelerators.
  210.        */
  211.  
  212.       case EXT_EXAMPLE:
  213.          {
  214.  
  215.          /* do a grep on the current word */
  216.  
  217.          char szWord[64];
  218.          edEditGetCurrentWord(hWnd,szWord,63);
  219.          if (szWord[0])
  220.             wsprintf(szCommand,"tee.com fgrep.com -M %s *.c",(LPSTR)szWord);
  221.          if (DialogBox(hInst,"CommandBox",hWnd,CommandDlgProc))
  222.             edRunCommand(hWnd, bWait, bCapture, szCommand);
  223.          return TRUE;
  224.          break;
  225.          }
  226.  
  227.       case EXT_IF:
  228.          {
  229.          /* Ctrl+I:  'C' template for   if (  )
  230.  
  231.           */
  232.          UINT wColNo;
  233.  
  234.          edEditInsertString(hWnd,"if (  )");
  235.  
  236.          wColNo  = edGetColumnNumber(hWnd);
  237.  
  238.          edEditGoToColumn(hWnd,wColNo-2);
  239.  
  240.          return TRUE;
  241.          break;
  242.          }
  243.  
  244.       case EXT_FOR:
  245.          {
  246.          /* Ctrl+F:  'C' template for   for ( ; ; )
  247.  
  248.           */
  249.          UINT wColNo;
  250.  
  251.          edEditInsertString(hWnd,"for ( ; ; )");
  252.  
  253.          wColNo  = edGetColumnNumber(hWnd);
  254.  
  255.          edEditGoToColumn(hWnd,wColNo-5);
  256.  
  257.          return TRUE;
  258.          break;
  259.          }
  260.  
  261.       case EXT_SWITCH:
  262.          {
  263.          /* Ctrl+S:  'C' template for   switch (  )
  264.                                            {
  265.  
  266.                                            }
  267.           */
  268.          UINT wColNo;
  269.          UINT wLineNo;
  270.  
  271.          edEditInsertString(hWnd,"switch (  )\r   {\r\r}");
  272.  
  273.          wColNo  = edGetColumnNumber(hWnd);
  274.          wLineNo = edGetLineNumber(hWnd);
  275.  
  276.          edEditGoToColumn(hWnd,wColNo+5);
  277.  
  278.          edEditGoToLine(hWnd,wLineNo-3);
  279.  
  280.          return TRUE;
  281.          break;
  282.          }
  283.  
  284.       default:
  285.  
  286.          /* return NULL to all messages not processed. */
  287.  
  288.          break;
  289.  
  290.       }  /* end switch (wParam) */
  291.  
  292.    return NULL;
  293.  
  294.    }
  295.  
  296.  
  297.  
  298. BOOL FAR PASCAL LibMain(HANDLE hInstance, UINT wDataSeg, UINT cbHeap, LPSTR lpszCmdLine)
  299.    {
  300.    return TRUE;
  301.  
  302.    NOREF(hInstance);
  303.    NOREF(wDataSeg);
  304.    NOREF(cbHeap);
  305.    NOREF(lpszCmdLine);
  306.    }
  307.  
  308.  
  309. int FAR PASCAL WEP(int nParameter)
  310.    {
  311.    return TRUE;
  312.  
  313.    NOREF(nParameter);
  314.    }
  315.  
  316.  
  317. BOOL FAR PASCAL CommandDlgProc(HWND hDlg, UINT msg, UINT wParam, LONG lParam)
  318.    {
  319.    switch (msg)
  320.       {
  321.       case WM_INITDIALOG:
  322.          CheckDlgButton(hDlg,IDD_WAIT,bWait);
  323.          CheckDlgButton(hDlg,IDD_CAPTURE,bCapture);
  324.          SetDlgItemText(hDlg,IDD_COMMAND,szCommand);
  325.  
  326.       case WM_COMMAND:
  327.          switch(wParam)
  328.             {
  329.             case IDOK:
  330.                bWait = IsDlgButtonChecked(hDlg,IDD_WAIT);
  331.                bCapture = IsDlgButtonChecked(hDlg,IDD_CAPTURE);
  332.                if (GetDlgItemText(hDlg,IDD_COMMAND,szCommand,sizeof(szCommand)))
  333.                   {
  334.                   EndDialog(hDlg,TRUE);
  335.                   break;
  336.                   }
  337.                /* else fall through */
  338.  
  339.             case IDCANCEL:
  340.                EndDialog(hDlg,FALSE);
  341.                break;
  342.  
  343.             default:
  344.                return (FALSE);
  345.             }
  346.          break;
  347.  
  348.       default:
  349.          return(FALSE);
  350.       }
  351.    return (TRUE);
  352.  
  353.    NOREF(lParam);
  354.    }
  355.  
  356.  
  357.  
  358. int InitMenu(HWND hWnd)
  359.    {
  360.    UINT wStatus;
  361.    HMENU hCurrentMenu;
  362.    POINT ptStart,ptEnd;
  363.  
  364.    hCurrentMenu = GetMenu(hWnd);
  365.  
  366.    /*  if there is a current selection, enable the cut & copy
  367.     *  commands.
  368.     */
  369.    wStatus = (UINT)edGetSelectionState(hWnd, &ptStart, &ptEnd);
  370.    if (!wStatus)
  371.       wStatus = MF_GRAYED;
  372.    else
  373.       wStatus = MF_ENABLED;
  374.    EnableMenuItem(hCurrentMenu, IDM_EDITCUT,  wStatus);
  375.    EnableMenuItem(hCurrentMenu, IDM_EDITCOPY, wStatus);
  376.  
  377.    /*  if there is text on the clipboard, enable the paste
  378.     *  command.
  379.     */
  380.    if (OpenClipboard(hWnd))
  381.       {
  382.       if (IsClipboardFormatAvailable(CF_TEXT)
  383.             || IsClipboardFormatAvailable(CF_OEMTEXT))
  384.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_ENABLED);
  385.       else
  386.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  387.       CloseClipboard();
  388.       }
  389.    else
  390.       EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  391.  
  392.    /* set the Undo, Redo, Insert, and WordWrap menu items */
  393.    wStatus = (UINT)edGetUndoState(hWnd);
  394.    if (!wStatus)
  395.       wStatus = MF_GRAYED;
  396.    else
  397.       wStatus = MF_ENABLED;
  398.    EnableMenuItem(hCurrentMenu, IDM_EDITUNDO, wStatus);
  399.  
  400.    wStatus = (UINT)edGetRedoState(hWnd);
  401.    if (!wStatus)
  402.       wStatus = MF_GRAYED;
  403.    else
  404.       wStatus = MF_ENABLED;
  405.    EnableMenuItem(hCurrentMenu, IDM_EDITREDO, wStatus);
  406.  
  407.    wStatus = (UINT)edGetWordWrapState(hWnd);
  408.    if (!wStatus)
  409.       wStatus = MF_UNCHECKED;
  410.    else
  411.       wStatus = MF_CHECKED;
  412.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEWRAP, MF_BYCOMMAND|wStatus);
  413.  
  414.    wStatus = (UINT)edGetInsertState(hWnd);
  415.    if (!wStatus)
  416.       wStatus = MF_UNCHECKED;
  417.    else
  418.       wStatus = MF_CHECKED;
  419.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEINS, MF_BYCOMMAND|wStatus);
  420.  
  421.    return TRUE;   /* we handled it, don't return 0 */
  422.    }
  423.